package com.lws.basic.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lws.basic.view.LoadingViewDialog;

/**
 * 碎片的基类
 */

public abstract class BaseFragment extends Fragment{
    private LoadingViewDialog mLoadingView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(setLayoutId(), container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    /**
     * @return
     * 导入布局
     */
    public abstract int setLayoutId();

    protected void initView(View rootView) {

    }
    protected void initData() {
    }

    protected void show(int content,Fragment fragment) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(content, fragment);
        transaction.commitAllowingStateLoss();//允许状态值丢失
    }

    public void showLoading(){
        mLoadingView = new LoadingViewDialog();
        mLoadingView.show(getFragmentManager(),"" );
    }
    public void showLoading(String content){
        mLoadingView = new LoadingViewDialog(content);
        mLoadingView.show(getFragmentManager(),"" );


    }

    public void dismissLoading(){
        if (mLoadingView != null){
            mLoadingView.dismiss();
        }

    }
}
